package org.smartboot.socket.client;

import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.common.ChatMessage;
import org.smartboot.socket.transport.AioSession;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * @author laoliu
 * @since 1.0
 */
public class ChatClientProcessor implements MessageProcessor<ChatMessage> {

    @Override
    public void process(AioSession s, ChatMessage msg) {
        System.out.println("读到信息："+msg);
//            try {
//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//                ObjectOutputStream objectOutput = new ObjectOutputStream(byteArrayOutputStream);
//                objectOutput.writeObject(msg);
//                byte[] data = byteArrayOutputStream.toByteArray();
//                s.writeBuffer().writeInt(data.length);
//                s.writeBuffer().write(data);
//                s.writeBuffer().flush();
//                ////objectOutput.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }

    }

    @Override
    public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
        switch (stateMachineEnum) {
            case NEW_SESSION:
                System.out.println("StateMachineEnum.NEW_SESSION");
                //clients.put(session.getSessionID(), session);
                break;
            case INPUT_SHUTDOWN:
                System.out.println("StateMachineEnum.INPUT_SHUTDOWN");
                break;
            case PROCESS_EXCEPTION:
                System.out.println("StateMachineEnum.PROCESS_EXCEPTION");
                break;
            case DECODE_EXCEPTION:
                System.out.println("StateMachineEnum.DECODE_EXCEPTION");
                break;
            case INPUT_EXCEPTION:
                System.out.println("StateMachineEnum.INPUT_EXCEPTION");
                break;
            case OUTPUT_EXCEPTION:
                System.out.println("StateMachineEnum.OUTPUT_EXCEPTION");
                break;
            case SESSION_CLOSING:
                System.out.println("StateMachineEnum.SESSION_CLOSING");
                break;
            case SESSION_CLOSED:
                System.out.println("StateMachineEnum.SESSION_CLOSED");
                // clients.remove(session.getSessionID());
                break;

            default:
                System.out.println("StateMachineEnum.default");
        }
    }
}
